package List排序;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;

public class 流式排序
{

    public static void main(String[] args) {
        // 修复：使用ArrayList包装Arrays.asList()结果，使其可变
        List<Employee> employees = new ArrayList<>(Arrays.asList(
                new Employee("E1001", "张三", "技术部", LocalDate.of(2020, 3, 15), 15000, 4),
                new Employee("E1002", "李四", "市场部", LocalDate.of(2022, 1, 10), 12000, 5),
                new Employee("E1003", "王五", "技术部", LocalDate.of(2021, 7, 22), 18000, 3),
                new Employee("E1004", "赵六", "财务部", LocalDate.of(2019, 5, 30), 16000, 4),
                new Employee("E1005", "钱七", "技术部", LocalDate.of(2020, 3, 15), 15000, 5)
        ));

        // 场景1：按薪资降序排序（高薪优先）
        System.out.println("=== 薪资排名榜 ===");
        employees.sort(Comparator.comparingDouble(Employee::getSalary).reversed());
        employees.forEach(e -> System.out.println(e.getName() + ": " + e.getSalary()));

        // 场景2：部门分组 + 组内按入职时间升序（老员工优先）
        System.out.println("\n=== 部门资深员工排名 ===");
        Map<String, List<Employee>> byDept = new HashMap<>();
        // 手动分组避免流操作复杂性
        for (Employee e : employees) {
            byDept.computeIfAbsent(e.getDepartment(), k -> new ArrayList<>()).add(e);
        }

        byDept.forEach((dept, list) -> {
            System.out.println("[" + dept + "]");
            list.sort(Comparator.comparing(Employee::getHireDate));
            list.forEach(emp -> System.out.println("  " + emp.getName() + " (" + emp.getHireDate() + ")"));
        });

        // 场景3：综合排序（绩效降序 > 薪资降序 > 工龄升序）
        System.out.println("\n=== 综合表现排名 ===");
        employees.sort(Comparator
                               .comparingInt(Employee::getPerformance).reversed()  // 1. 绩效从高到低
                               .thenComparing(Comparator.comparingDouble(Employee::getSalary).reversed()  // 2. 薪资从高到低
                                                      .thenComparing(Employee::getHireDate)  // 3. 入职日期从早到晚（工龄从长到短）
                               ));

        employees.forEach(e -> System.out.printf(
                "%s | 绩效:%d | 薪资:%.0f | 工龄:%d年%n",
                e.getName(), e.getPerformance(), e.getSalary(),
                ChronoUnit.YEARS.between(e.getHireDate(), LocalDate.now())
        ));

        // 场景4：带空值安全的排序（部门可能为null）
        System.out.println("\n=== 安全排序（处理null值）===");
        employees.add(new Employee("E1006", "孙八", null, LocalDate.now(), 14000, 4));

        employees.sort(Comparator
                               .comparing(Employee::getDepartment,
                                          Comparator.nullsLast(String::compareTo))  // null部门排最后
                               .thenComparing(Employee::getName)
        );

        employees.forEach(e -> System.out.printf(
                "部门:%s | 姓名:%s%n",
                e.getDepartment() != null ? e.getDepartment() : "未分配",
                e.getName()
        ));
    }
}

class Employee {
    private String id;
    private String name;
    private String department;
    private LocalDate hireDate;
    private double salary;
    private int performance;

    public Employee(String id, String name, String department,
                    LocalDate hireDate, double salary, int performance) {
        this.id = id;
        this.name = name;
        this.department = department;
        this.hireDate = hireDate;
        this.salary = salary;
        this.performance = performance;
    }

    // Getter方法
    public String getId() { return id; }
    public String getName() { return name; }
    public String getDepartment() { return department; }
    public LocalDate getHireDate() { return hireDate; }
    public double getSalary() { return salary; }
    public int getPerformance() { return performance; }
}